  //var api = require("../../utils/api.js"),
  var app = getApp();
  //Util = require('../../utils/util.js'),
  var CityPlugin = require('../../components/city/index');
  var showLocationDailog = false, isChinaCity = false, showNoDataDialog = false;
  var currentPage;//当前小程序页面
  var mRefresh = false
  Page({
    data: {
      isLocal: false,//是否定位成功
      currentCityId: "36",
      currentCityName: "北京",
      searchCityId: "36",
      searchCityName: "北京",
      lat: "0",
      lng: "0",
      indicatordots: true,
      selectedTabIndicator: 'recommand',
      SceneryList: [],
      sceneryindex: 0,
      cityInfo: {
        cityId: "",
        cityName: ""
      },
      keywords: {
        "type": "",
        "data": {}
      },
      scrollHeight:667,
    },
    getLocationCity: function() {
      var _this = this;
          //_api = api;
      var qqMap = app.getQQMap();
      var _app = app;
      var lat, lng;
     // api.Location.get({
        wx.getLocation({
          success: function(res) {
              __wxConfig.debug && console.log("homepage Location res = ", res)
              lat = res.latitude;
              lng = res.longitude;
              qqMap.reverseGeocoder({
                  location: {
                      latitude: res.latitude,
                      longitude: res.longitude
                  },
                  success: function(res) {
                      __wxConfig.debug && console.log("homepage qq map reverseGeocoder success res = ",res);
                      //遍历城市解析出城市id
                      var currentCityName = res.result.address_component.city//北京市
                      __wxConfig.debug && console.log("homepage qq map reverseGeocoder success currentCityName = ",currentCityName);
                      //將定位的城市顯示出來
                      _this.searchCityName = currentCityName
                      _this.setData({
                        isLocal: true,
                        searchCityName: currentCityName,
                      })
                      _this.getHomePageData(currentCityName);
                  },
                  fail: function(res) {
                    __wxConfig.debug && console.log("homepage qq map reverseGeocoder fail res = ", res);
                  }
              });
          },
          fail: function(e) {
              __wxConfig.debug && console.log('homepage location fail');
              _this.setData({
                  isLocal:false,
                  searchCityId:_this.data.searchCityId,
                  searchCityName:_this.data.searchCityName,
              });
              callback();
          }
      })
    },
    
    onLoad: function (options) {
      var that = this;
      that.loadData();
    },

    onShow: function (){
      var that = this;
      var city;
      const cityHistory = wx.getStorageSync('cityHistory')
      __wxConfig.debug && console.log("homepage cityHistory", cityHistory)
      this.setData({
        searchCityName: cityHistory[0] || that.searchCityName,
      })
      that.city = cityHistory[0] || that.searchCityName,
        __wxConfig.debug && console.log("this cityHistory", that.city)
      that.getHomePageData(that.city);
    },

    loadData:function(){
        var that = this;
        var hasCurrentSelectCity = true
        const cityHistory = wx.getStorageSync('cityHistory')
        __wxConfig.debug && console.log("homepage cityHistory", cityHistory)
        that.getLocationCity();
    },

    getHomePageData: function (currentCityName) {
      var that = this;
      __wxConfig.debug && console.log("homepage getHomePageData", currentCityName)
      wx.request({
        url: app.globalData.globalReqUrl + '/music/getScenic',
        data: {
          city: currentCityName
        },
        header: {
          'content-type': 'application/x-www-form-urlencoded' // 默认值
        },
        method: 'POST',
        success: function (res) {
          __wxConfig.debug && console.log("homepage getHomePageData 数据：", res)
          if (res.data.content != -1) {
            __wxConfig.debug && console.log("homepage getHomePageData sceneryList：", res.data.content)
            that.setData({
              sceneryList: res.data.content,
            });
          } else {
            hasNextMusic = false;
            console.log("请求城市景点失败")
            wx.showToast({
              title: '未查询到城市景点信息',
              icon: 'none',
              duration: 2000
            })
          }
        },
        fail: function () {
          console.log("请求城市景点失败")
        }
      })
    },
    
    onCitySelectClick: function () {
      var _this = this;
      CityPlugin({
        title: "",
        cityId: ""
      }, function (res) {
        _this.setData({
          searchCityId: res.value.cityId || '',
          searchCityName: res.value.cityName || '',
          cityInfo: {
            cityId: res.value.cityId || "",
            cityName: res.value.cityName || ""
          }
        })
        _this.getHomePageData();
      })
    },
    onPullDownRefresh:function(){
       mRefresh = true
       this.loadData();
    },
    stopRefresh:function(){
       if(mRefresh) {
         wx.stopPullDownRefresh()
       }
    }
  });

